package hluck.me.srorage.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hluck.me.srorage.bus.Vo.GoodsVo;
import hluck.me.srorage.bus.domain.BusGoods;
import hluck.me.srorage.bus.domain.BusProvider;
import hluck.me.srorage.bus.service.BusGoodsService;
import hluck.me.srorage.bus.service.BusProviderService;
import hluck.me.srorage.sys.common.Constast;
import hluck.me.srorage.sys.common.DataGridView;
import hluck.me.srorage.sys.common.MyFileUtils;
import hluck.me.srorage.sys.common.ResultObj;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 老雷
 * @since 2021-02-15
 */
@RestController
@RequestMapping("/goods")
public class BusGoodsController {
    @Autowired
    BusGoodsService goodsService;
    @Autowired
    BusProviderService providerService;

    /**
     * 查询
     */
    @RequestMapping("loadAllGoods")
    public DataGridView loadAllGoods(GoodsVo goodsVo){
        Page<BusGoods> page = new Page<>(goodsVo.getPage(), goodsVo.getLimit());
        QueryWrapper<BusGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(goodsVo.getProviderid()!=null&&goodsVo.getProviderid()!=0,"providerid",goodsVo.getProviderid());
        queryWrapper.like(StringUtils.isNotBlank(goodsVo.getGoodsname()),"goodsname",goodsVo.getGoodsname());
        queryWrapper.like(StringUtils.isNotBlank(goodsVo.getProductcode()),"productcode",goodsVo.getProductcode());
        queryWrapper.like(StringUtils.isNotBlank(goodsVo.getPromitcode()),"promitcode",goodsVo.getPromitcode());
        queryWrapper.like(StringUtils.isNotBlank(goodsVo.getDescription()),"description",goodsVo.getDescription());
        queryWrapper.like(StringUtils.isNotBlank(goodsVo.getSize()),"size",goodsVo.getSize());

        this.goodsService.page(page,queryWrapper);
        List<BusGoods> goods = page.getRecords();
        for (BusGoods busGoods : goods) {
            BusProvider provider = providerService.getById(busGoods.getProviderid());
            if (null!=provider) {
                busGoods.setProvidername(provider.getProvidername());
            }
        }
        return new DataGridView(page.getTotal(),goods);
    }

    /**
     * 添加商品
     */
    @RequestMapping("addGoods")
    public ResultObj addGoods(GoodsVo goodsVo){
        try {
            if (goodsVo.getGoodsimg()!=null&&goodsVo.getGoodsimg().endsWith("_temp")) {
                String newName=MyFileUtils.renameFile(goodsVo.getGoodsimg());
                goodsVo.setGoodsimg(newName);
            }
            goodsService.save(goodsVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }
    /**
     * 更新商品
     */
    @RequestMapping("updateGoods")
    public ResultObj updateGoods(GoodsVo goodsVo){
        try {
            //不是默认图片
            if (!(goodsVo.getGoodsimg()!=null&&goodsVo.getGoodsimg().equals(Constast.DEFAULT_IMAGE))) {
                if (goodsVo.getGoodsimg().endsWith("_temp")){
                    String newName=MyFileUtils.renameFile(goodsVo.getGoodsimg());
                    goodsVo.setGoodsimg(newName);
                    //删除原先图片
                    String oldPath = goodsService.getById(goodsVo.getId()).getGoodsimg();
                        MyFileUtils.removeFileByPath(oldPath);
                }
            }
            goodsService.updateById(goodsVo);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除商品
     */
    @RequestMapping("deleteGoods")
    public ResultObj deleteGoods(Integer id,String goodsimg){
        try{
            //删除商品图片
            MyFileUtils.removeFileByPath(goodsimg);
            this.goodsService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

}

